<template>
    <!-- 对话框表单 -->
    <el-dialog
        custom-class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate == 'add' || baTable.form.operate == 'edit'"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('goods.category_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.category_id"
                        prop="category_id"
                        :input-attr="{
                            field: 'cate',
                            'remote-url': category + 'index',
                            pk: 'id',
                            placeholder: t('Please select field', { field: t('goods.category_id') }),
                        }"
                    />
                    <FormItem
                        :label="t('goods.goods_name')"
                        type="string"
                        v-model="baTable.form.items!.goods_name"
                        prop="goods_name"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.goods_name') }) }"
                    />
                    <FormItem
                        :label="t('goods.slider_images')"
                        type="images"
                        v-model="baTable.form.items!.slider_images"
                        prop="slider_images"
                        :input-attr="{ placeholder: t('Please select field', { field: t('goods.slider_images') }) }"
                    />
                    <FormItem
                        :label="t('goods.virtual')"
                        type="number"
                        prop="virtual"
                        v-model.number="baTable.form.items!.virtual"
                        :input-attr="{ step: '1', placeholder: t('Please input field', { field: t('goods.virtual') }) }"
                    />
                    <FormItem
                        :label="t('goods.stock')"
                        type="number"
                        prop="stock"
                        v-model.number="baTable.form.items!.stock"
                        :input-attr="{ step: '1', placeholder: t('Please input field', { field: t('goods.stock') }) }"
                    />
                    <FormItem
                        :label="t('goods.coupon_ids')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.coupon_ids"
                        prop="coupon_ids"
                        :input-attr="{
                            field: 'coupon_name',
                            'remote-url': coupon + 'index',
                            pk: 'coupon.id',
                            multiple: true,
                            placeholder: t('Please select field', { field: t('goods.coupon_ids') }),
                        }"
                    />
                    <FormItem
                        :label="t('goods.content_images')"
                        type="images"
                        v-model="baTable.form.items!.content_images"
                        prop="content_images"
                        :input-attr="{ placeholder: t('Please select field', { field: t('goods.content_images') }) }"
                    />
                    <FormItem
                        :label="t('goods.commission1')"
                        type="string"
                        prop="commission1"
                        v-model="baTable.form.items!.commission1"
                        :input-attr="{ step: '0.01', placeholder: '10%代表返10%，10元/10代表返10元' }"
                    />
                    <FormItem
                        :label="t('goods.commission2')"
                        type="string"
                        prop="commission2"
                        v-model="baTable.form.items!.commission2"
                        :input-attr="{ step: '0.01', placeholder: '10%代表返10%，10元/10代表返10元' }"
                    />
                    <FormItem
                        :label="t('goods.commission3')"
                        type="string"
                        prop="commission3"
                        v-model="baTable.form.items!.commission3"
                        :input-attr="{ step: '0.01', placeholder: '10%代表返10%，10元/10代表返10元' }"
                    />
                    <FormItem
                        :label="t('goods.price')"
                        type="number"
                        prop="price"
                        v-model.number="baTable.form.items!.price"
                        :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('goods.price') }) }"
                    />
                    <FormItem
                        :label="t('goods.integral')"
                        type="number"
                        prop="integral"
                        v-model.number="baTable.form.items!.integral"
                        :input-attr="{ step: '1', placeholder: t('Please input field', { field: t('goods.integral') }) }"
                    />
                    <FormItem
                        label="可抵扣积分"
                        type="number"
                        prop="use_score"
                        v-model.number="baTable.form.items!.use_score"
                        :input-attr="{ step: '1', placeholder: t('Please input field', { field: '可抵扣积分' }) }"
                    />
                    <FormItem
                        :label="t('goods.post_price')"
                        type="number"
                        prop="post_price"
                        v-model.number="baTable.form.items!.post_price"
                        :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('goods.post_price') }) }"
                    />
                    <FormItem
                        :label="t('goods.old_price')"
                        type="number"
                        prop="old_price"
                        v-model.number="baTable.form.items!.old_price"
                        :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('goods.old_price') }) }"
                    />
                    <FormItem
                        :label="t('goods.vip_price')"
                        type="number"
                        prop="vip_price"
                        v-model.number="baTable.form.items!.vip_price"
                        :input-attr="{ step: '0.01', placeholder: t('Please input field', { field: t('goods.vip_price') }) }"
                    />
                    <FormItem
                        :label="t('goods.best_switch')"
                        type="switch"
                        v-model="baTable.form.items!.best_switch"
                        prop="best_switch"
                        :input-attr="{ placeholder: t('Please input field', { field: t('goods.best_switch') }) }"
                    />
                    <FormItem
                        label="是否新品"
                        type="switch"
                        v-model="baTable.form.items!.new_switch"
                        prop="new_switch"
                        :input-attr="{ placeholder: t('Please input field', { field: '新品' }) }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { category, coupon } from '/@/api/controllerUrls'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    category_id: [buildValidatorData('required', t('goods.category_id'))],
    goods_name: [buildValidatorData('required', t('goods.goods_name'))],
    slider_images: [buildValidatorData('required', t('goods.slider_images'))],
    virtual: [buildValidatorData('required', t('goods.virtual'))],
    stock: [buildValidatorData('required', t('goods.stock'))],
    content_images: [buildValidatorData('required', t('goods.content_images'))],
    price: [buildValidatorData('required', t('goods.price'))],
    integral: [buildValidatorData('required', t('goods.integral'))],
    post_price: [buildValidatorData('required', t('goods.post_price'))],
    old_price: [buildValidatorData('required', t('goods.old_price'))],
    vip_price: [buildValidatorData('required', t('goods.vip_price'))],
})
</script>

<style scoped lang="scss"></style>
